#include "comm.hpp"

int main() {
  // 1.创建匿名管道
  bool n = creatNamePipe(NMAE_PIPE);
  assert(n);
  (void)n;

  // 打开管道
  std::cout << "server开始" << std::endl;
  int fd = open(NMAE_PIPE, O_RDONLY);
  std::cout << "server打开成功" << std::endl;

  assert(fd >= 0);

  char buf[1024];
  while (1) {
    // 读取成功返回读取的字节数
    int s = read(fd, buf, sizeof(buf) - 1);
    if (s > 0) {
      // 读到了n个，下标为最后一个元素下标为n-1。再把后一个置0
      buf[s] = 0;
      std::cout << "client->server: " << buf << std::endl;
    } else if (s == 0) {
      std::cout << "client退出，我也退出" << std::endl;
      break;
    }
  }

  close(fd);
  removeFifo(NMAE_PIPE);
  return 0;
}
